﻿using MockJson.Database;
using MockJson.Properties;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MockJson
{
    public partial class FrmSetting : Form
    {
        bool initialed = false;
        private readonly Action _action;

        public FrmSetting(Action action)
        {
            InitializeComponent();

            var settings = App.DB.Queryable<Setting>().ToList();
            txtIP.Text = App.GetValue("IP", "localhost", settings);
            txtPort.Text = App.GetValue("PORT", "8099", settings);
            var autoStart = App.GetValue("AUTOSTART", "1", settings);
            cbAutoStart.Checked = autoStart == "1" ? true : false;
            initialed = true;
            _action = action;
        }

        private void txtIP_TextChanged(object sender, EventArgs e)
        {
            if (initialed)
                App.WriteValue("IP", txtIP.Text);
        }

        private void txtPort_TextChanged(object sender, EventArgs e)
        {
            if (initialed)
                App.WriteValue("PORT", txtPort.Text);
        }

        private void cbAutoStart_CheckedChanged(object sender, EventArgs e)
        {
            if (initialed)
            App.WriteValue("AUTOSTART", cbAutoStart.Checked ? "1" : "0");
        }

        private void FrmSetting_FormClosing(object sender, FormClosingEventArgs e)
        {
            _action.Invoke();
        }
    }
}
